package edu.jamestang.yeb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.jamestang.yeb.entity.Menu;
import edu.jamestang.yeb.mapper.MenuMapper;
import edu.jamestang.yeb.service.MenuService;
import edu.jamestang.yeb.util.AdminUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author JamesTang
 * @date 2021/12/13 23:21
 */

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public List<Menu> getMenusByAdminId() {

        Integer adminId = AdminUtil.getCurrentAdmin().getId();
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        List<Menu> menus = (List<Menu>) valueOperations.get("menu_" + adminId);

        if(CollectionUtils.isEmpty(menus)){
            menus = menuMapper.getMenusByAdminId(adminId);
            valueOperations.set("menu_"+adminId, menus);
        }
        return menus;
    }


    @Override
    public List<Menu> getMenusByRole() {
        return menuMapper.getMenusByRole();
    }

    @Override
    public List<Menu> getAllMenus() {

        return menuMapper.getAllMenus();
    }
}
